package com.neusoft.controller;

import com.neusoft.domain.VirtualWallet;
import com.neusoft.po.CommonResult;
import com.neusoft.service.ClientWalletService;
import com.neusoft.service.CreditBillService;
import com.neusoft.service.PenaltyService;
import com.neusoft.exception.InsufficientFundsException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/WalletController")
public class ClientWalletController {

    @Autowired
    private ClientWalletService walletClientService;
    @Autowired
    private CreditBillService creditBillService;
    @Autowired
    private PenaltyService penaltyService;

    // 使用信用额度
    @PostMapping("/use-credit/{userId}/{amount}")
    public CommonResult useCredit(@PathVariable Long userId, @PathVariable BigDecimal amount) throws InsufficientFundsException {
        walletClientService.useCredit(userId, amount);
        return new CommonResult(200, "success", amount);
    }

    // 支付
    @PostMapping("/pay/{userId}/{merchantUserId}/{amount}")
    public CommonResult pay(@PathVariable Long userId,
                            @PathVariable Long merchantUserId,
                            @PathVariable BigDecimal amount) {
        try {
            walletClientService.pay(userId, merchantUserId, amount);
            return new CommonResult(200, "success", "支付成功");
        } catch (InsufficientFundsException e) {
            // 处理余额不足异常
            return new CommonResult(400, "error", e.getMessage());
        } catch (Exception e) {
            // 处理其他异常
            return new CommonResult(500, "error", "支付失败：" + e.getMessage());
        }
    }

    // 获取用户信用额度
    @GetMapping("/credit-limit/{userId}")
    public CommonResult getCreditLimit(@PathVariable Long userId) {
        BigDecimal creditLimit = walletClientService.getCreditLimit(userId);
        return new CommonResult(200, "success", creditLimit);
    }

    // 获取未偿还账单
    @GetMapping("/outstanding-bill/{userId}")
    public CommonResult getOutstandingBill(@PathVariable Long userId) {
        BigDecimal outstandingBill = creditBillService.getOutstandingBill(userId);
        return new CommonResult(200, "success", outstandingBill);
    }

    // 还款
    @PostMapping("/pay-bill/{userId}/{paymentAmount}")
    public CommonResult payBill(@PathVariable Long userId, @PathVariable BigDecimal paymentAmount) {
        creditBillService.payBill(userId, paymentAmount);
        return new CommonResult(200, "success", "还款成功：" + paymentAmount);
    }

    // 获取滞纳金
    @GetMapping("/penalty/{userId}")
    public CommonResult getPenalty(@PathVariable Long userId) {
        BigDecimal penalty = penaltyService.calculatePenalty(userId);
        return new CommonResult(200, "success", penalty);
    }

    // 充值
    @PostMapping("/deposit/{userId}/{amount}")
    public CommonResult deposit(@PathVariable Long userId, @PathVariable BigDecimal amount) {
        walletClientService.deposit(userId, amount);
        return new CommonResult(200, "success", "充值成功");
    }

    // 提现
    @PostMapping("/withdraw/{userId}/{amount}")
    public CommonResult withdraw(@PathVariable Long userId, @PathVariable BigDecimal amount) {
        try {
            walletClientService.withdraw(userId, amount);
            return new CommonResult(200, "success", "提现成功");
        } catch (InsufficientFundsException e) {
            return new CommonResult(400, "error", "余额不足");
        }
    }

    // 获取余额
    @GetMapping("/balance/{userId}")
    public CommonResult getBalance(@PathVariable Long userId) {
        BigDecimal balance = walletClientService.getBalance(userId);
        return new CommonResult(200, "success", balance);
    }

    // 转账
    @PostMapping("/transfer/{fromUserId}/{toUserId}/{amount}")
    public CommonResult transfer(@PathVariable Long fromUserId, @PathVariable Long toUserId, @PathVariable BigDecimal amount) {
        try {
            walletClientService.transfer(fromUserId, toUserId, amount);
            return new CommonResult(200, "success", "转账成功");
        } catch (InsufficientFundsException e) {
            return new CommonResult(400, "error", "余额不足");
        }
    }

    // 初始化用户钱包
    @PostMapping("/initialize/{userId}")
    public CommonResult initializeWallet(@PathVariable Long userId) {
        walletClientService.initializeWallet(userId);
        return new CommonResult(200, "success", "钱包初始化成功");
    }

    // 获取用户钱包信息
    @GetMapping("/{userId}")
    public CommonResult getWalletByUserId(@PathVariable Long userId) {
        VirtualWallet wallet = walletClientService.getWalletByUserId(userId);
        if (wallet == null) {
            return new CommonResult(404, "error", "钱包未找到");
        }
        return new CommonResult(200, "success", wallet);
    }
}